# -*- coding: utf-8 -*-
import scrapy
from ..items import ImgRenameItem


class ImgClassifySpider(scrapy.Spider):
    name = 'ImgClassify'
    allowed_domains = ['lab.scrapyd.cn']
    start_urls = ['http://lab.scrapyd.cn/archives/55.html',
                  'http://lab.scrapyd.cn/archives/57.html',
                  ]

    def parse(self, response):
        # 实例化item
        item = ImgRenameItem()
        # 注意imgurls是一个集合也就是多张图片
        img_urls = response.xpath('//div[contains(@class,"post-content")]/p/img/@src').extract()
        item['img_url'] = img_urls
        # 抓取文章标题作为图集名称
        img_file_name = response.xpath('//h1[contains(@class,"post-title")]/a/text()').extract_first()
        print(img_file_name)
        item['img_file_name'] = img_file_name
        yield item

